package com.atguigu.service.impl;

import com.atguigu.base.BaseDao;
import com.atguigu.base.BaseServiceImpl;
import com.atguigu.dao.PermissionDao;
import com.atguigu.dao.RolePermissionDao;
import com.atguigu.entity.Permission;
import com.atguigu.entity.RolePermission;
import com.atguigu.helper.PermissionHelper;
import com.atguigu.service.PermissionService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
@Transactional
public class PermissionServiceImpl extends BaseServiceImpl<Permission> implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private RolePermissionDao rolePermissionDao;

    @Override
    protected BaseDao<Permission> getEntityDao() {
        return permissionDao;
    }

    @Override
    public List<Permission> findAllMenu() {
        //全部权限列表
        List<Permission> permissionList = permissionDao.findAll();
        if(CollectionUtils.isEmpty(permissionList)) return null;

        //构建树形数据,总共三级
        //把权限数据构建成树形结构数据
        List<Permission> result = PermissionHelper.bulid(permissionList);
        return result;
    }

    @Override
    public List<String> findCodeListByAdminId(Long id) {
        if(id ==1){
            return permissionDao.findAllCodeList();

        }
        return permissionDao.findCodeListByAdminId(id);
    }

    @Override
    public List<Map<String, Object>> findPermissionByRoleId(Long roleId) {
        // 查询全部权限List<Permission>
        List<Permission> permissionList =permissionDao.findAll();
        // 查询当前角色所有的权限id List<Long>
        List<Long> permissionIdList =rolePermissionDao.findPermissionIdListByRoleId(roleId);

        List<Map<String, Object>> zNodes =new ArrayList<>();
        for (Permission permission : permissionList) {
            Map<String,Object> node=new HashMap<>();
            node.put("id",permission.getId());
            node.put("pId",permission.getParentId());
            node.put("name",permission.getName());
            if(permissionIdList.contains(permission.getId())){
                node.put("checked",true);
            }
            zNodes.add(node);
        }
        //var zNodes = [[${zNodes}]];
        /*var zNodes =[
            { id:1, pId:0, name:"随意勾选 1",open:false},
            { id:11, pId:1, name:"随意勾选 1-1", open:false},
            { id:111, pId:11, name:"随意勾选 1-1-1",checked:true},
            { id:112, pId:11, name:"随意勾选 1-1-2",checked:true},
            { id:12, pId:1, name:"随意勾选 1-2", open:true},
            { id:121, pId:12, name:"随意勾选 1-2-1"},
            { id:122, pId:12, name:"随意勾选 1-2-2"},
            { id:2, pId:0, name:"随意勾选 2", checked:true, open:true},
            { id:21, pId:2, name:"随意勾选 2-1"},
            { id:22, pId:2, name:"随意勾选 2-2", open:true},
            { id:221, pId:22, name:"随意勾选 2-2-1", checked:true},
            { id:222, pId:22, name:"随意勾选 2-2-2"},
            { id:23, pId:2, name:"随意勾选 2-3"}
        ];*/

        return zNodes;
    }

    @Override
    public void saveRolePermissionRealtionShip(Long roleId, Long[] permissionIds) {
        // 删除role的所有权限
        rolePermissionDao.deleteByRoleId(roleId);
        // 重新增加荣的所有权限
        if(null != permissionIds && permissionIds.length>0){
            for (Long permissionId : permissionIds) {
                if (StringUtils.isEmpty(permissionId)) continue;
                RolePermission rolePermission =new RolePermission();
                rolePermission.setRoleId(roleId);
                rolePermission.setPermissionId(permissionId);
                rolePermissionDao.insert(rolePermission);

            }
        }


    }
}
